
public class ATM {
    
    private boolean userAuthenticated;
    private int currentAccountNumber;
    private Screen screen;
    private Keypad keypad;
    private CashDispenser cashDispenser;
    private DepositSlot depositSlot;
    private BankDatabase bankDatabase;
    
    private static final int BALANCE_INQUIRY = 1;
    private static final int WITHDRAWAL = 2;
    private static final int DEPOSIT = 3;
    private static final int EXIT = 4;
    
    // default constructo initialized with default value
    public ATM(){
	
	this.userAuthenticated = false; //svn poprawka
	this.currentAccountNumber = 0; // no current account number
	this.screen = new Screen();
	this.keypad = new Keypad();
	this.cashDispenser = new CashDispenser();
	this.depositSlot = new DepositSlot();
	this.bankDatabase = new BankDatabase();
	
    }
    
    public void run(){
	// Welcome and authenticated user
	while(true){
	    // loop while user is not autenticated
	    while( !userAuthenticated ){
		screen.displayMessageLine("\n Welcome");
		authenticateUser();
	    }
	}
    }

    private void authenticateUser() {
	screen.displayMessage("\n please enter the account number");
	int accountNumber = keypad.getInput();
	screen.displayMessage("\n Enter your pin: ");
	int pin = keypad.getInput();
	
	// zmieniamy zmienna boll na podstawie odpowiedzi z bankDatabse
	userAuthenticated = bankDatabase.authenticateUser(accountNumber, pin);
	
	if( userAuthenticated ){
	    currentAccountNumber = accountNumber;
	}
	else{
	    screen.displayMessage("\n Invalid Account number . Please Try again");
	}
    }
    
    /**
     * Display menu and perform Transaction
     */
    private void performTransaction(){
	// local variable to stroe transaction beeing proceeded
	Transaction currentTransaction = null;
	
	boolean userExited = false; // user choose to Exit?
	
	while( !userExited ){
	    // show main menu ang get user selectiuon
	    int mainMenuSelection = displayMainMenu();
	    
	    switch( mainMenuSelection ){
	    	case BALANCE_INQUIRY:
	    	case DEPOSIT:
	    	case WITHDRAWAL:
	    	    // initialize new object 
	    	    currentTransaction = createTransaction(mainMenuSelection);
	    	    currentTransaction.execute();
	    	    break;
	    	case EXIT:
	    	    screen.displayMessageLine("Exiting ......");
	    	    userExited = true;
	    	    break;
	    	default :
	    	    screen.displayMessageLine("You enter not valid number . Please type again");
	    	    break;
	    }
	}
    }

    private Transaction createTransaction(int type) {
	Transaction temp = null;
	
	switch(type){
	case BALANCE_INQUIRY:
	    temp = new BalanceInquiry(currentAccountNumber, screen, bankDatabase);
	    break;
	case DEPOSIT:
	    temp = new Deposit(currentAccountNumber, screen, bankDatabase, keypad, depositSlot);
	    break;
	case WITHDRAWAL:
	    temp = new Withdrawal(currentAccountNumber, screen, bankDatabase, keypad, cashDispenser);
	    break;
	}// end switch
	
	return temp;
    }

    private int displayMainMenu() {
	screen.displayMessageLine("\nMenu:");
	screen.displayMessageLine("1 - View My Balance");
	screen.displayMessageLine("2 - Withdraw ");
	screen.displayMessageLine("3 - Deposit");
	screen.displayMessageLine("4 - EXIT\n");
	
	return keypad.getInput();
    }

}
